#include "sqlite_pool.h"

SQLITE_Pool::SQLITE_Pool(QObject *parent)
    : QObject{parent}
{
    reset_Material_Height_DataBase();

    reset_Order_Process_DataBase();

    // reset_QR_Code_Process_DataBase();
}

SQLITE_Pool *SQLITE_Pool::getInstance()
{
    static SQLITE_Pool instance;
    return &instance;
}

void SQLITE_Pool::reset_Material_Height_DataBase()
{
    if(m_Material_Height_DataBase)
    {
        m_Material_Height_DataBase->close();

        m_Material_Height_DataBase->removeDatabase("MaTerial_Height_DataBase");

        delete m_Material_Height_DataBase;

        m_Material_Height_DataBase = nullptr;
    }

    m_Material_Height_DataBase = new QSqlDatabase();

    *m_Material_Height_DataBase = QSqlDatabase::addDatabase("QSQLITE",QString("MaTerial_Height_DataBase"));

    m_Material_Height_DataBase->setDatabaseName("./MaTerial_Height.db");
    if(!m_Material_Height_DataBase->open())
    {
        qDebug()<<"Cpp:m_Material_Height_DataBase->lastError():"<<m_Material_Height_DataBase->lastError().text().toUtf8().data();
    }
    else
    {
        qDebug()<<"Cpp:连接数据库 : MaTerial_Height.db 成功";
    }

    qDebug()<<"Cpp: 重置数据库  MaTerial_Height.db 成功";
}

QSqlDatabase *SQLITE_Pool::get_Material_Height_DataBase()
{
    return m_Material_Height_DataBase;
}

void SQLITE_Pool::reset_Order_Process_DataBase()
{
    if(m_Order_Process_DataBase)
    {
        m_Order_Process_DataBase->close();

        m_Order_Process_DataBase->removeDatabase("Order_Process_DataBase");

        delete m_Order_Process_DataBase;

        m_Order_Process_DataBase = nullptr;
    }

    m_Order_Process_DataBase = new QSqlDatabase();

    *m_Order_Process_DataBase = QSqlDatabase::addDatabase("QSQLITE",QString("Order_Process_DataBase"));

    m_Order_Process_DataBase->setDatabaseName("./Order_Process.db");
    if(!m_Order_Process_DataBase->open())
    {
        qDebug()<<"Cpp:m_Order_Process_DataBase->lastError():"<<m_Order_Process_DataBase->lastError().text().toUtf8().data();
    }
    else
    {
        qDebug()<<"Cpp:连接数据库 : Order_Process.db 成功";
    }

    qDebug()<<"Cpp: 重置数据库  Order_Process.db 成功";
}

QSqlDatabase *SQLITE_Pool::get_Order_Process_DataBase()
{
    return m_Order_Process_DataBase;
}

void SQLITE_Pool::reset_QR_Code_Process_DataBase()
{
    if(m_QR_Code_Process_DataBase)
    {
        m_QR_Code_Process_DataBase->close();

        m_QR_Code_Process_DataBase->removeDatabase("QR_Code_Process_DataBase");

        delete m_QR_Code_Process_DataBase;

        m_QR_Code_Process_DataBase = nullptr;
    }

    m_QR_Code_Process_DataBase = new QSqlDatabase();

    *m_QR_Code_Process_DataBase = QSqlDatabase::addDatabase("QSQLITE",QString("QR_Code_Process_DataBase"));

    m_QR_Code_Process_DataBase->setDatabaseName("./QR_Code_Process.db");
    if(!m_QR_Code_Process_DataBase->open())
    {
        qDebug()<<"Cpp:m_QR_Code_Process_DataBase->lastError():"<<m_QR_Code_Process_DataBase->lastError().text().toUtf8().data();
    }
    else
    {
        qDebug()<<"Cpp:连接数据库 : QR_Code_Process.db 成功";
    }

    qDebug()<<"Cpp: 重置数据库  QR_Code_Process.db 成功";
}

QSqlDatabase *SQLITE_Pool::get_QR_Code_Process_DataBase()
{
    return m_QR_Code_Process_DataBase;
}
